package com.ruoyi.location.utils;

import com.ruoyi.location.pojo.Location;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class MyUtils {
    /*
      按照日期升序排序
     */
    public static List<Location> sortByDate(List<Location> locations){
        Collections.sort(locations,new Comparator<Location>() {
            @Override
            public int compare(Location o1, Location o2) {
                Date d1 = stringToDate(o1.getLocationTime());
                Date d2 = stringToDate(o2.getLocationTime());
                if(d1.before(d2)) return -1;
                else
                    return 1;
            }
        });
        return locations;
    }
    /*
       字符串转时间
     */
    public static Date stringToDate(String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }
}
